package com.lxy.demo.controller;

import com.lxy.demo.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
public class controller01 {

    @GetMapping("/hello")
    public String Hello(@RequestParam(value = "name",required = false,defaultValue = "错误") String userName, Model model){
        model.addAttribute("username",userName);
        return "index";
    }

    @PostMapping("/post")
    public String Post(@RequestParam(value = "userName",required = true) String name,
                       @RequestParam(required = true) String password,
                       @RequestParam(defaultValue = "155") Integer height,
                       Model model){
        model.addAttribute("userName",name);
        model.addAttribute("password",password);
        model.addAttribute("height",height);
        return "index";
    }

    //restFull风格
    @RequestMapping(value = "/user/{id}/{name}")
    public String getUser(@PathVariable("id") Integer uid,
                          @PathVariable("name") String name,
                          Model model){
        model.addAttribute("id",uid);
        model.addAttribute("name",name);
        return "index";
    }

    @RequestMapping("/users")
    public String user(Users users,Model model){
        model.addAttribute("users",users);
        return "index";
    }

    //返回json格式@ResponseBody
    @RequestMapping("/user")
    @ResponseBody
    public Users lookUser(){
        return new Users("1", "hhh", "男", 15);
    }

    //json集合格式
    @RequestMapping("/userList")
    @ResponseBody
    public List<Users> lookUserList(){
        Users users1 = new Users("1", "hhh", "男", 15);
        Users users2 = new Users("2", "www", "女", 15);
        Users users3 = new Users("3", "hhh", "男", 15);
        List<Users> users = new ArrayList<>();
        users.add(users1);
        users.add(users2);
        users.add(users3);
        return users;
    }

    //对返回的数据进行封装
    @RequestMapping("/userListMap")
    @ResponseBody
    public Map<String,Object> getUsersListMap(){
        Map<String,Object> map = new HashMap<>();
        try {
            Users users = new Users("1001", "郭德纲", "110", 44);
            Users users1 = new Users("1002", "郭德纲1", "110", 55);
            Users users2 = new Users("1003", "郭德纲2", "110", 66);
            List<Users> list = new ArrayList<>();
            list.add(users);
            list.add(users1);
            list.add(users2);
            int i = 1/0;
            map.put("data",list); //data封装数据
            map.put("code",200); //code封装状态码
            map.put("msg","数据返回成功！"); //msg提示信息
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500); //code封装状态码
            map.put("msg","数据返回失败！服务器问题！");
        }
        return map;
    }

    //重定向和转发
    @RequestMapping("/test01")
    public String goTest02(){
        return "forward:/test02";//转发
        //return "redirect:/test02"; 重定向
    }

    @ResponseBody
    @RequestMapping("/test02")
    public String Test02(){
        return "This is test02";
    }

    @GetMapping("/getDate")
    @ResponseBody
    public String getDate(Date date){
        System.out.println(date);
        return date.toLocaleString();
    }

    //接收前端的异步请求
    @GetMapping("/getUsers")
    @ResponseBody
    public Users getUsers(Integer id, String name){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        Users users = new Users("1003", "郭德纲2", "110", 66,new Date());
        return users;
    }
}
